Connecting to Google Drive

Description

You can create named storage connection strings for Google Drive. Google Drive storage connections can be used with the Xbasic storage functions to read files from and save files to a folder in Google Drive.

Setting up Google Drive

Before you build a storage connection string for Google Drive, you need to enable the Google Drive API in the Google Cloud console and add a Service Account. The Service Account will need permissions to access your Google Drive folder(s).

  1. Go to the Google Cloud console and create a new project. If you already have an existing project, you can use that project.

  2. Enable the Google Drive api for your project.

  3. Create a service account.

  4. Create a key for the service account using the JSON option. Download the JSON file.

  5. Get the email address for the service account and share the Google Drive folder with the email address of your service account. If you want to upload files to Google Drive, you will need to grant Edit access to the service account.

  6. Locate the id of the Google Drive folder you want to use in your storage connection string. The id is found after the https://drive.google.com/drive/folders/ portion of the URL for a Google Drive folder.

Defining the Google Drive Storage Connection

Once you have completed all of the above, you can then create a named storage connection string.

To create the names storage connection string you will need:

  • The JSON that you downloaded when you created new keys for your service account.
  • The id for the folder you want to point to with your named storage connection string.
  1. Open the Named Storage Connection builder by selecting Tools > Storage Connection strings on the Web Projects Control Panel

  2. Click New to create a new Storage Connection. Click the Build button in the Add New Storage Connection dialog to open the Storage Connection builder.

  3. In the Storage Connection String Builder, select Google from the list of Storage Providers.

  4. Enter the folder id in the Google Folder Id.

    Using Load Credentials From file, load the JSON file you downloaded when you created a key for your service account.

    Google Drive Storage Provider Settings
    Google Drive Storage Provider Settings
  5. Optionally configure Advanced Settings... (see below).

  6. Click Test to verify your Google Drive settings are valid.

  7. Click OK to save the configuration. Give the Storage Connection a name. Then click OK to save the Storage Connection String.

Once you have created a named storage connection string for Google Drive you can then use any of the a5storage* functions (except a5storage_copyFiles()) to read and save files to Google Drive.

Handling Duplicate Objects in Google Drive

When you save a file to Google Drive, by default duplicate objects are not created. For example, you upload a file called c:\mydata\file1.txt and a file named "file1.txt" already exists in the Google Drive folder, the existing "file1.txt" will be replaced by "c:\mydata\file1.txt".

The Advanced Settings... option in the Storage Connection dialog can be used to configure what happens when a duplicate object is encountered when listing files and downloading files from Google Drive.

  • What is a "duplicate" object?

    When you save a new object in Google Drive, the object is automatically given a unique id (the object id). So, if you upload a file and give it an existing object name (called a short name), the new object does not overwrite the existing object. Instead it will be given a unique object Id. The fully qualified object name will be a combination of the short name name you specified and the unique object id (automatically assigned). The syntax for the object name is the short name followed by the object Id in parentheses. For example: short name(object Id).

    If you select the Require ID option in the Advanced Settings, the fully qualified object name is required in order to retrieve or delete an object when duplicate objects exist. For example, assume you have uploaded a file twice with a short name of "file1.txt". Google Drive will have two objects with the names, e.g. file1.txt(1234) and file1.txt(12345) where 1234 and 12345 are the unique object ids that were assigned to the object when the objects were created.

    When you use the a5storage* functions to list, retrieve, or edit storage objects, you can specify a fully qualified object name.

    When you list objects in the bucket, duplicate objects are fully qualified. If an object is not a duplicate only the short name is shown.

    On the other hand, if you use the Pick Newest option you do need to specify fully qualified object names. You can use short names and if there is a duplicate, the newest object will be selected. When you list objects in the bucket, only short names will be shown (even if the bucket contains objects with duplicate short names).

    Handling Duplicate objects in Google Drive
    Handling Duplicate objects in Google Drive
  • The following advanced options are available to configure the Google Drive file behavior:

    Property
    Description
    Handle Duplicate

    Defines how to handle duplicate items in a Google Drive folder:

    • Require ID - Requires including the object ID in the filename when multiple files in the Google Drive folder have the same filename. If duplicate names exist, the a5storage_listFiles() function will return fully qualified names that include the file ID for any duplicate files in the drive. The format of the fully qualified name is filename(object ID).
    • Pick Newest - If multiple files in the Google Drive folder have the same name, chooses the file that was most recently modified. The a5storage_listFiles() function will return the short name form of the files in the drive.
    Timeout

    The number of milliseconds to wait for a response from Google Drive before giving up. Defaults to "Default".

    Number of Tries

    If accessing the Google Drive fails, how many times should the action be retried. Defaults to 3.

    Follow Redirects

    Whether or not to follow redirects. Typically not needed in most situations. Defaults to "Default".

    Number of Redirects

    The number of redirects to follow before giving up. Defaults to 10.

Revoking Google Drive Access

Access to your Google Drive can be restricted or revoked at any time by changing the folder's sharing permissions for the Service Account.

  1. In Google Drive, open the Share permissions for the directory the Service Account can access.

  2. To restrict access to read only, change the Service Account's permissions from "Editing" to "View".

  3. To revoke access, remove the Service Account from the list of accounts that can access the folder.

Xbasic Examples

A Google Drive storage connection can be used with the Xbasic Storage functions. The examples below demonstrate basic upload, download, delete, and file listing.

  • Uploading a File

    An example of uploading a file to Google Drive using a5Storage_saveFile().

    dim filename as c = "D:\images\aalogo.bmp"
    dim itemname as c = "aalogo2.bmp"
    dim result as p
    dim callResult as L
    
    callResult = a5Storage_saveFile("googledrive",filename,itemname,"",result)
    
    ? callResult
    = .T.
    
    ? result
    = AbsolutePath = "aalogo2.bmp(1NZO9pAVnr-gpnzCe7ijty3hGXGsaNbLU)"
    ContentType = "image/bmp"
    hasError = .F.
    ModifiedTime = 10/04/2022 11:36:41 88 am
    Name = "aalogo2.bmp(1NZO9pAVnr-gpnzCe7ijty3hGXGsaNbLU)"
    size = 15258
    timeTakenMilliseconds = 1819
  • Downloading a File

    An example of downloading a file from Google Drive using a5storage_getitem_as_blob().

    dim objectname as c = "aalogo2.bmp"
    dim result as p
    dim callResult as L
    dim file as B
    
    file = a5storage_getitem_as_blob("googledrive",objectname,result)
    
    ?result
    = AbsolutePath = "aalogo2.bmp(1NZO9pAVnr-gpnzCe7ijty3hGXGsaNbLU)"
    ContentType = ""
    hasError = .F.
    ModifiedTime = 10/04/2022 11:36:41 88 am
    Name = "aalogo2.bmp(1NZO9pAVnr-gpnzCe7ijty3hGXGsaNbLU)"
    size = 15258
    timeTakenMilliseconds = 1002
    
    ?file
    = 0000 : 42 4d 9a 3b 00 00 00 00 00 00 8a 00 00 00 7c 00 
    0010 : 00 00 78 00 00 00 3f 00 00 00 01 00 10 00 03 00 
    0020 : 00 00 10 3b 00 00 23 2e 00 00 23 2e 00 00 00 00 
    0030 : 00 00 00 00 00 00 00 f8 00 00 e0 07 00 00 1f 00 
    0040 : 00 00 00 00 00 00 42 47 52 73 00 00 00 00 00 00 
    0050 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    0060 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    0070 : 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 
    0080 : 00 00 00 00 00 00 00 00 00 00 ff ff ff ff ff ff 
    0090 : ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 
    ''... truncated ...

    To send the file to the client from an Ajax Callback, see a5Helper_generateFileDownloadJS Function. See also Context.Response.SendFile.

  • Listing Files

    An example of listing files in a Google Drive folder using a5Storage_listItems().

    dim files as c
    files =  a5Storage_listItems("googledrive")
    
    ? files
    = aalogo2.bmp
    aalogo3.bmp(1hF5PODG0-1MJ9Nqzkg9d5KjlOq0B2dKt)
    aalogo3.bmp(1zyK895-K6lHJkaY31TskXPgZvcqD0r-H)
    D:\images\aalogo.bmp
    drivedemo
    inPlaceEdit_DataSeries.png
    itemA.jpg
  • Deleting a File

    An example of deleting files from Google Drive using a5storage_deleteItem().

    dim objectname as c = "aalogo2.bmp(1NZO9pAVnr-gpnzCe7ijty3hGXGsaNbLU)"
    dim result as p
    dim callResult as L
    
    callResult = a5storage_deleteItem("googledrive",objectname,result)
    
    ? callResult
    = .T.
    
    ? result
    = AbsolutePath = "aalogo2.bmp(1NZO9pAVnr-gpnzCe7ijty3hGXGsaNbLU)"
    ContentType = ""
    hasError = .F.
    ModifiedTime = 10/04/2022 11:36:41 88 am
    Name = "aalogo2.bmp(1NZO9pAVnr-gpnzCe7ijty3hGXGsaNbLU)"
    size = 15258
    timeTakenMilliseconds = 721
    
    ? a5storage_listItems("googledrive")
    = aalogo3.bmp(1hF5PODG0-1MJ9Nqzkg9d5KjlOq0B2dKt)
    aalogo3.bmp(1zyK895-K6lHJkaY31TskXPgZvcqD0r-H)
    D:\images\aalogo.bmp
    drivedemo
    inPlaceEdit_DataSeries.png
    itemA.jpg

    If the account that deletes an object from the Google Drive folder is not the account that added the object, the object is still visible to all other users who have access to the Google Drive folder. See Why deleted file still shows up in Google Drive (Stack Overflow) for an explanation.

Videos

Creating a Google Drive Storage Connection

In this video we show how you can create a named connection string for Google Dive and then how you can read/write files in your Google Drive.

2022-02-25

See Also